<template>
  <div class="pb50" v-loading="loading">
    <el-form
      size="small"
      ref="form"
      :model="form"
      label-width="150px"
      v-if="!loading"
    >
      <!--基础设置-->
      <Basic :form="form"></Basic>
      <!--活动时间-->
      <Datetime :form="form"></Datetime>
      <!--活动商品-->
      <ProductIndex :form="form" :type="'edit'"></ProductIndex>
      <!--其它-->
      <Other :form="form"></Other>
    </el-form>

    <!--提交-->
    <div class="common-button-wrapper">
      <el-button size="small" @click="cancelFunc">取消</el-button>
      <el-button
        size="small"
        type="primary"
        @click="onSubmit"
        :loading="loading"
        >提交</el-button
      >
    </div>
  </div>
</template>
<script>
import SeckillApi from "@/api/seckill.js";
import Basic from "./part/Basic.vue";
import Datetime from "./part/Datetime.vue";
import ProductIndex from "./part/Product.vue";
import Other from "./part/Other.vue";
import { mergeTable } from "@/utils/table.js";
export default {
  components: {
    /*基础设置*/
    Basic,
    /*活动时间*/
    Datetime,
    /*活动商品*/
    ProductIndex,
    /*其它*/
    Other,
  },
  data() {
    return {
      /*是否正在加载*/
      loading: true,
      /*表单对象*/
      form: {
        /*活动ID*/
        seckillActivityId: null,
        /*图片ID*/
        imageId: 0,
        /*活动日期*/
        startDate: "",
        endDate: "",
        /*活动时间*/
        activeTime: "",
        /*标题*/
        title: "",
        /*是否生效，默认1为生效，0为不生效*/
        status: 1,
        /*默认排序*/
        sort: 100,
        /*商品源数据*/
        tableData: [],
        /*商品列表*/
        productList: [],
      },
    };
  },
  created() {
    /*获取列表*/
    this.form.seckillActivityId = this.$route.query.seckillActivityId;
    this.getData();
  },
  methods: {
    /*获取数据*/
    getData() {
      let self = this;
      self.loading = true;
      SeckillApi.getActive(
        { seckillActivityId: self.form.seckillActivityId },
        true
      )
        .then((res) => {
          let tempForm = res.data;
          tempForm.seckillActivityId = self.form.seckillActivityId;
          tempForm.productDelIds = [];
          tempForm.tableData = [];
          tempForm.startDate = res.data.startTime;
          tempForm.endDate = res.data.endTime;
          tempForm.activeTime = [];
          tempForm.activeTime[0] = tempForm.dayStartTime;
          tempForm.activeTime[1] = tempForm.dayEndTime;
          let arr = [];
          for (let i = 0; i < res.data.productList.length; i++) {
            let item = res.data.productList[i];
            for (let j = 0; j < item.seckillSku.length; j++) {
              let sku = item.seckillSku[j];
              let obj = {
                productName: item.product.productName,
                productId: item.product.productId,
                productPrice: item.product.productPrice,
                stockNum: item.product.productStock,
                specType: item.product.specType,
                isDelete: item.isDelete,
                productSkuId: sku.productSkuId,
                specName: sku.productAttr,
                salesNum: item.totalSales,
                seckillPrice: sku.seckillPrice,
                seckillStock: sku.seckillStock,
                seckillProductId: sku.seckillProductId,
                limitNum: item.limitNum,
                sort: item.sort,
                seckillProductSkuId: sku.seckillProductSkuId,
                salesInitial: item.salesInitial,
                joinNum: item.joinNum,
              };
              arr.push(obj);
            }
          }
          tempForm.tableData = mergeTable(arr);
          self.form = tempForm;
          self.loading = false;
        })
        .catch((error) => {});
    },

    /*提交表单*/
    onSubmit() {
      let self = this;
      if (self.form.endDate == null || self.form.endDate == "") {
        ElMessage({
          message: "请填写活动结束日期",
          type: "error",
        });
        return;
      }

      self.$refs.form.validate((valid) => {
        if (valid) {
          self.form.productList = self.tableFormet(self.form.tableData);
          let params = self.form;
          delete params.tableData;
          params.dayStartTime = params.activeTime[0];
          params.dayEndTime = params.activeTime[1];
          self.loading = true;
          SeckillApi.saveActive(params, true)
            .then((data) => {
              self.loading = false;
              ElMessage({
                message: "修改成功",
                type: "success",
              });
              self.cancelFunc();
            })
            .catch((error) => {
              self.loading = false;
            });
        }
      });
    },

    /*表格数据格式化*/
    tableFormet(list) {
      let specList = [];
      let newList = [];
      let curItem = null;
      for (let i = 0; i < list.length; i++) {
        let item = list[i];
        let obj = {
          productSkuId: item.productSkuId,
          seckillStock: item.seckillStock,
          seckillPrice: item.seckillPrice,
          productAttr: item.specName,
          seckillProductSkuId: item.seckillProductSkuId,
          productPrice: item.productPrice,
          salesInitial: item.salesInitial,
        };
        if (curItem == null) {
          curItem = item;
        }
        if (curItem.productId != item.productId) {
          curItem.specList = specList;
          newList.push(curItem);
          curItem = item;
          specList = [];
        }
        specList.push(obj);
      }
      curItem.specList = specList;
      newList.push(curItem);
      return newList;
    },

    /*取消*/
    cancelFunc() {
      this.$router.back(-1);
    },
  },
};
</script>
